package com.google.fbyoutube.client.widget;

import java.util.List;

import com.google.fbyoutube.client.YouTube;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PalMainCell extends Composite {

	public PalMainCell() {
		
		VerticalPanel hp = new VerticalPanel();
		gridTitle = new SimplePanel();
		hp.add(gridTitle);
		hp.add(grid);
		
		setTitle("Top Videos");
		
		initWidget(hp);
		
		grid.setCellSpacing(5);
		grid.setSize("735px", "600px");
		
		WidThumb widThumb_16 = new WidThumb(null);
		grid.setWidget(0, 0, widThumb_16);
		
		WidThumb widThumb_17 = new WidThumb(null);
		grid.setWidget(0, 1, widThumb_17);
		
		WidThumb widThumb_18 = new WidThumb(null);
		grid.setWidget(0, 2, widThumb_18);
		
		WidThumb widThumb_19 = new WidThumb(null);
		grid.setWidget(0, 3, widThumb_19);
		
		WidThumb widThumb_21 = new WidThumb(null);
        grid.setWidget(0, 4, widThumb_21);
		
		WidThumb widThumb_12 = new WidThumb(null);
		grid.setWidget(1, 0, widThumb_12);
		
		WidThumb widThumb_13 = new WidThumb(null);
		grid.setWidget(1, 1, widThumb_13);
		
		WidThumb widThumb_14 = new WidThumb(null);
		grid.setWidget(1, 2, widThumb_14);
		
		WidThumb widThumb_15 = new WidThumb(null);
		grid.setWidget(1, 3, widThumb_15);
		
		WidThumb widThumb_151 = new WidThumb(null);
        grid.setWidget(1, 4, widThumb_151);
		
		WidThumb widThumb_8 = new WidThumb(null);
		grid.setWidget(2, 0, widThumb_8);
		
		WidThumb widThumb_9 = new WidThumb(null);
		grid.setWidget(2, 1, widThumb_9);
		
		WidThumb widThumb_10 = new WidThumb(null);
		grid.setWidget(2, 2, widThumb_10);
		
		WidThumb widThumb_112 = new WidThumb(null);
		grid.setWidget(2, 3, widThumb_112);
		
		WidThumb widThumb_11 = new WidThumb(null);
        grid.setWidget(2, 4, widThumb_11);
		
		WidThumb widThumb_4 = new WidThumb(null);
		grid.setWidget(3, 0, widThumb_4);
		
		WidThumb widThumb_5 = new WidThumb(null);
		grid.setWidget(3, 1, widThumb_5);
		
		WidThumb widThumb_6 = new WidThumb(null);
		grid.setWidget(3, 2, widThumb_6);
		
		WidThumb widThumb_7 = new WidThumb(null);
		grid.setWidget(3, 3, widThumb_7);
		
		WidThumb widThumb_71 = new WidThumb(null);
        grid.setWidget(3, 4, widThumb_71);
		
		grid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(2, 2, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(2, 3, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
		grid.getCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
		
		
	}
	
	public void reload(List<ytVideoEntry> videos) {
		grid.clear();
		YouTube.getAppGUI().complelteLoading();
		if (videos == null || videos.size() == 0) {
			// TODO: 
			// No videos get from feeds, display some messages here
			// throw new RuntimeException("No videos got from the feed!");
			for (int i = 0; i < 20; ++i) {
				WidThumb widThumb_i = new WidThumb(null);
				grid.setWidget(i/COL, i%COL, widThumb_i);
				grid.getCellFormatter().setHorizontalAlignment(i/COL, i%COL, HasHorizontalAlignment.ALIGN_CENTER);
			}
		} else {
			for (int i = 0; i < COL * ROW; ++i) {
				WidThumb widThumb_i;
				if (i < videos.size()) {
					widThumb_i = new WidThumb(videos.get(i));
				} else {
					widThumb_i = new WidThumb(null);
				}
				SimplePanel sp = new SimplePanel();
				sp.add(widThumb_i);
				
				if (i/COL >= grid.getRowCount()) {
					grid.resize(i/COL + 1, COL);
				}
				
				grid.setWidget(i/COL, i%COL, sp);
				grid.getCellFormatter().setHorizontalAlignment(i/COL, i%COL, HasHorizontalAlignment.ALIGN_CENTER);
			}
		}
	}
	
	public void setTitle(String text) {
		gridTitle.setWidget(new HTML("<div style='padding: 10px;'><h3>" + text + "</h3></div>"));
	}
	
	private static final int COL = 5;
	private static final int ROW = 5;
	Grid grid = new Grid(ROW, COL);
	private SimplePanel gridTitle;

}
